﻿/*
 * Date：2023/04/21
 * Subject：订单主表信息
 * Author：Max
 * 
 * 
 * **/
using Max.PH5_2010A.Shopping.Domain.Shared;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2010A.Shopping.Domain
{
    /// <summary>
    /// 订单主表信息（你、什么时间、花了多少钱、这些商品邮寄到哪里）
    /// </summary
    [Table("tb_order_master")]
    public class OrderMasterModel : AuditField<Guid>
    {
        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "订单号")]
        public string OrderNo { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "会员ID")]
        public Guid MemberID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "收件人姓名")]
        public string RecivedName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "收件人电话")]
        public string RecivedPhone { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "收件人详细地址")]
        public string RecivedAddress { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "订单总金额")]
        public decimal OrderAccount { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "折扣优惠金额")]
        public decimal RateAccount { get; set; } = 0;

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "支付金额")]
        public decimal? PayAccount { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "支付时间")]
        public DateTime? PayTime { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "支付方式")]
        public PayType? PayType { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "订单状态")]
        public OrderState OrderState { get; set; } = OrderState.WaittingPay;

        /// <summary>
        /// 
        /// </summary>
        [Display(Description = "订单创建日期")]
        public DateTime OrderCreatedTime { get; set; }

        [NotMapped]
        public string OrderCreatedTimeDisplay
        {
            get
            {
                return this.OrderCreatedTime.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        [NotMapped]
        public string OrderStateNameDisplay
        {
            get
            {
                var t = this.OrderState.GetType();
                var f = t.GetField(this.OrderState.ToString());
                var objects = f.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), true);
                if(objects != null || objects.Length > 0)
                {
                    var n = ((System.ComponentModel.DataAnnotations.DisplayAttribute)objects[0]).Description;
                    return n;
                }
                return "<el-button size=\"mini\" type=\"danger\" >立即支付</el-button>";
            }
        }


        public OrderMasterModel()
        {
            this.OrderNo = CreateOrderNo();
        }

        /// <summary>
        /// 模拟生成订单号（用业务流水号，不用雪花ID、GUID）
        /// </summary>
        /// <returns></returns>
        private string CreateOrderNo(OrderNoType t = OrderNoType.FlowNubmer)
        {
            string OrderNo = "";
                    string type = "P";
            switch (t)
            {
                case OrderNoType.Snowflake:
                    OrderNo = new Snowflake.Core.IdWorker(1,1).NextId().ToString();
                    break;
                case OrderNoType.Guid:
                    OrderNo = Guid.NewGuid().ToString("N");
                    break;
                case OrderNoType.FlowNubmer:
                default:
                    string date = System.DateTime.Now.ToString("yyyyMMddHHmmss");
                    string no = new Random().Next(1000, 9999).ToString().PadLeft(4, '0');
                    OrderNo = $"{date}{no}";
                    break;
            }
            return $"{type}{OrderNo}";
        }
    }
}
